<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {
	
	var $per_product;

	public function __construct()
	{
		parent::__construct();

		$this->template->set('nav', $this->uri->segment(1));
		$this->template->set('title', 'Bài viết');
		$this->template->set('section', 'Tạo bài viết');

		$this->load->model('New_model');
	}

	public function index()
	{
		$data['news'] = $this->New_model->infos();

		$this->template->load('admin/template','admin/setting',$data);
	}

	public function edit($id = false)
	{
		$addBtn = $this->input->post('add');		

		$data['new'] = $this->New_model->info($id);

		if($addBtn)
		{
			$data = array(
					'title'			=>	$this->input->post('title'),
					'detail'		=>	$this->input->post('detail'),
				);

			$this->New_model->update($data,$id);

			redirect('setting');
		}
		else
		{
			$this->template->load('admin/template','admin/editNew',$data);
		}
	}

	public function add()
	{
		$addBtn = $this->input->post('add');

		$data = array();

		if($addBtn)
		{
			$data = array(
					'title'			=>	$this->input->post('title'),
					'detail'		=>	$this->input->post('detail'),
				);

			$this->New_model->insert($data);

			redirect('setting');
		}
		else
		{
			$this->template->load('admin/template','admin/addNew',$data);
		}
	}
}